package org.will.common.metric;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by EdifierWill on 2016/9/14.
 */
public class TestCounter {

    MetricRegistry _metrics = new MetricRegistry();
    ConsoleReporter _consoleReporter = ConsoleReporter.forRegistry(_metrics).build();
    final Slf4jReporter _slf4jReporter = Slf4jReporter.forRegistry(_metrics)
            .outputTo(LoggerFactory.getLogger("A1"))
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();

    Map<String,Meter> _selfMeters;

    static TestCounter _instance;

    public static TestCounter getInstance(){
        if(_instance==null){
            _instance = new TestCounter();
        }

        return _instance;
    }

    public TestCounter(){
        _consoleReporter.start(3, TimeUnit.SECONDS);
        _slf4jReporter.start(3, TimeUnit.SECONDS);
        _selfMeters = new HashMap<>();
    }

    public void Mark(String name){

        if(!_selfMeters.containsKey(name)){
            _selfMeters.put(name, _metrics.meter(name));
        }
        _selfMeters.get(name).mark();
    }
}
